#! /bin/sh

een=$1
count="0"
raw="0"

if [ -z $een ] ; then een="-h"; fi

if [ $een = '-h' ] ; then
   echo "Usage smallsmart [options] filename";
   echo "  creates weighted indexes from small files"
   echo "Nota Bene: everything is first converted to lowercase";
   exit 0
fi


bestand=$1

tr 'A-Z' 'a-z' < $bestand | tr -sc '0-9a-z_\012-' ' ' > /tmp/bestand

aant_rec=`grep -wc rec /tmp/bestand`

#================================

awk '{
if ($1=="rec")
   {
   for (woord in kortlijst) checklijst[woord]=1;
   }
   else
   {
   for (x=1;x<=NF;x++)
       {
       found=0;
       for (woord in kortlijst) 
             if (woord==$x) 
                   if (checklijst[woord]) 
                           {
                           kortlijst[woord]++;checklijst[woord]=0;found=1;
                           }
       
       if (!found) 
             {
             kortlijst[$x]=1;
             checklijst[$x]=0;
             }
       }
   }
}
END {
for (woord in kortlijst) printf("%d %s\n",kortlijst[woord],woord);
}' /tmp/bestand

exit

#================================

awk 'BEGIN{
while (getline < "/tmp/lijst" > 0)
   {
   rij[$2]=$1;
   aantal_woorden++;
   }
rec=0;
}

#=================================
function schrijf_index(rec){
max_tf=0;
for (woord in kortlijst) 
           if (kortlijst[woord]> max_tf) max_tf=kortlijst[woord];
for (woord in kortlijst) 
    {
    for ( w in rij) if (w==woord) df=rij[w];
    tf=kortlijst[woord];

    new_tf=0.5 + 0.5 (tf/max_tf);
    new_wt=new_tf * log (N/df);
    S=0;
    for (w in kortlijst) S+=(kortlijst[w]^2);
    new_wt=new_wt / sqrt(S);
    printf("%d %s %f\n", rec,woord,new_wt);
    }
}

#================================
{
if ($1=="rec") {schrijf_index(rec);rec=$2;}
   else
   {
   for (x=1;x<=NF;x++)
       {
       found=0;
       for (woord in kortlijst) if (woord==$x) {kortlijst[woord]++;found=1;}
       if (!found) kortlijst[$x]=1;
       }
   }
}
#=================================
END{
schrijf_index(rec);
}' N=$aant_rec /tmp/bestand

#rm /tmp/lijst
#rm /tmp/bestand

